package com.kaibes.orm.core.util;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.kaibes.core.base.util.StringUtils;
import com.kaibes.orm.core.annotation.Column;
import com.kaibes.orm.core.core.ColumnField;
import com.kaibes.orm.core.core.TableColumn;

public class ColumnUtils {

    public static Map<String, Field> getColumnMap(Class<?> clazz) {
        Field[] fields = clazz.getDeclaredFields();
        Map<String, Field> map = new HashMap<>();
        for (Field field : fields) {
            String name = null;
            if (field.isAnnotationPresent(Column.class)) {
                Column column = field.getDeclaredAnnotation(Column.class);
                if (column.ignored()) {
                    continue;
                }
                if (!column.value().isEmpty()) {
                    name = column.value();
                }
            }
            if (name == null) {
                name = StringUtils.humpToLine(field.getName());
            }
            field.setAccessible(true);
            map.put(name, field);
        }
        return map;
    }
    
    public static List<ColumnField> getColumnFieldList(Class<?> clazz) {
        List<ColumnField> columnFields = new ArrayList<>();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            String name = null;
            if (field.isAnnotationPresent(Column.class)) {
                Column column = field.getDeclaredAnnotation(Column.class);
                if (column.ignored()) {
                    continue;
                }
                if (!column.value().isEmpty()) {
                    name = column.value();
                }
            }
            if (name == null) {
                name = StringUtils.humpToLine(field.getName());
            }
            field.setAccessible(true);
            ColumnField columnMap = new ColumnField();
            columnMap.setField(field);
            columnMap.setName(name);
            columnFields.add(columnMap);
        }
        return columnFields;
    }

    /*
     * public static void init(Class<?> clazz, List<String> columnNames, List<Field>
     * columnFields) { Field[] fields = clazz.getDeclaredFields(); for (Field field
     * : fields) { if (field.isAnnotationPresent(Column.class)) { String name =
     * field.getDeclaredAnnotation(Column.class).value(); if (!name.isEmpty()) {
     * field.setAccessible(true); columnNames.add(name); columnFields.add(field); }
     * } else { String name = field.getName(); name = StringUtils.humpToLine(name);
     * field.setAccessible(true); columnNames.add(name); columnFields.add(field); }
     * }
     * 
     * }
     */
    
    public static ColumnField getIdField(Class<?> clazz) {
        ColumnField idField = null;
        Field[] fields = clazz.getDeclaredFields();
        boolean isPrimary = false;
        for (Field field : fields) {
            String name = null;
            if (field.isAnnotationPresent(Column.class)) {
                Column column = field.getDeclaredAnnotation(Column.class);
                if (column.ignored()) {
                    continue;
                }
                if (!column.value().isEmpty()) {
                    name = column.value();
                }
                isPrimary = column.primary();
            }
            if (name == null) {
                name = StringUtils.humpToLine(field.getName());
            }
            if (isPrimary || "id".equals(name)) {
                idField = new ColumnField();
                idField.setName(name);
                idField.setField(field);
                break;
            }
        }
        return idField;
    }
    
    public static TableColumn getTableColumn(Class<?> clazz) {
        TableColumn tableColumn = new TableColumn();
        List<ColumnField> columnFields = new ArrayList<>();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            String name = null;
            boolean isPrimary = false;
            if (field.isAnnotationPresent(Column.class)) {
                Column column = field.getDeclaredAnnotation(Column.class);
                if (column.ignored()) {
                    continue;
                }
                if (!column.value().isEmpty()) {
                    name = column.value();
                }
                isPrimary = column.primary();
            }
            if (name == null) {
                name = StringUtils.humpToLine(field.getName());
            }
            field.setAccessible(true);
            ColumnField columnMap = new ColumnField();
            if (isPrimary || "id".equals(name)) {
                tableColumn.setIdColumn(columnMap);
            }
            columnMap.setField(field);
            columnMap.setName(name);
            columnFields.add(columnMap);
        }
        tableColumn.setEntityClass(clazz);
        tableColumn.setColumnFields(columnFields);
        return tableColumn;
    }

}
